using Game.Component.Operation;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class OperationDisplayerItem : BaseItem
	{
		public enum Type
		{
			None = 0,
			Main = 1,
			Game1 = 2,
			Game2 = 3,
			Game3 = 4,
			Game4 = 5,
			Game5 = 6,
			Game6 = 7,
			Game7 = 8,
			Game8 = 9
		}

		private enum State
		{
			Closing = 0,
			Running = 1
		}

		private State state;

		private Type type;

		private float SPEED = 5f;

		private float MIN_Y = 4.8f;

		private float MAX_Y = 14.3f;

		private float modelX;

		private float modelY;

		private float curVal;

		private float targetVal;

		private Transform selectObj;

		private OperationComponent selectScript;

		private Transform main;

		private Transform game1;

		private Transform game2;

		private Transform game3;

		private Transform game4;

		private Transform game5;

		private Transform game6;

		private Transform game7;

		private Transform game8;

		private Transform btnUp;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			main = model.Find("Main");
			game1 = model.Find("Game1");
			game2 = model.Find("Game2");
			game3 = model.Find("Game3");
			game4 = model.Find("Game4");
			game5 = model.Find("Game5");
			game6 = model.Find("Game6");
			game7 = model.Find("Game7");
			game8 = model.Find("Game8");
			btnUp = model.Find("BtnUp");
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (curVal > targetVal)
			{
				curVal -= Time.deltaTime * SPEED;
				if (curVal <= targetVal)
				{
					curVal = targetVal;
				}
				model.SetLocalY(curVal);
			}
			else if (curVal < targetVal)
			{
				curVal += Time.deltaTime * SPEED;
				if (curVal >= targetVal)
				{
					curVal = targetVal;
				}
				model.SetLocalY(curVal);
			}
			else
			{
				RemoveAudioPlayer();
			}
		}

		public void SetRunningState()
		{
			model.SetLocalY(MIN_Y);
			SetState(State.Running);
		}

		public void OpenDisplayer()
		{
			curVal = model.localPosition.y;
			targetVal = MIN_Y;
			SetState(State.Running);
		}

		public void CloseDisplayer()
		{
			curVal = model.localPosition.y;
			targetVal = MAX_Y;
			SetState(State.Closing);
		}

		public void ChangeType(Type val)
		{
			if (type != val)
			{
				type = val;
				ResetSelectObject();
				if (type == Type.Main)
				{
					CreateNewObject(main);
					selectScript = selectObj.gameObject.AddComponent<MainComponent>();
				}
				else if (type == Type.Game1)
				{
					CreateNewObject(game1);
					selectScript = selectObj.gameObject.AddComponent<Game1Component>();
				}
				else if (type == Type.Game2)
				{
					CreateNewObject(game2);
					selectScript = selectObj.gameObject.AddComponent<Game2Component>();
				}
				else if (type == Type.Game3)
				{
					CreateNewObject(game3);
					selectScript = selectObj.gameObject.AddComponent<Game3Component>();
				}
				else if (type == Type.Game4)
				{
					CreateNewObject(game4);
					selectScript = selectObj.gameObject.AddComponent<Game4Component>();
				}
				else if (type == Type.Game5)
				{
					CreateNewObject(game5);
					selectScript = selectObj.gameObject.AddComponent<Game5Component>();
				}
				else if (type == Type.Game6)
				{
					CreateNewObject(game6);
					selectScript = selectObj.gameObject.AddComponent<Game6Component>();
				}
				else if (type == Type.Game7)
				{
					CreateNewObject(game7);
					selectScript = selectObj.gameObject.AddComponent<Game7Component>();
				}
				else if (type == Type.Game8)
				{
					CreateNewObject(game8);
					selectScript = selectObj.gameObject.AddComponent<Game8Component>();
				}
				if (selectScript != null)
				{
					selectScript.displayer = this;
				}
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			if (state != 0)
			{
				RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
				if (raycastHit2D.collider != null && raycastHit2D.collider.transform == btnUp)
				{
					CloseDisplayer();
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				}
				else if (selectScript != null)
				{
					selectScript.TouchDown(touchEventData);
				}
			}
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			if (selectScript != null)
			{
				selectScript.TouchMove(touchEventData);
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			if (selectScript != null)
			{
				selectScript.TouchUp(touchEventData);
			}
		}

		private void CreateNewObject(Transform demo)
		{
			selectObj = Object.Instantiate(demo.gameObject).transform;
			selectObj.SetParent(model);
			selectObj.localScale = demo.localScale;
			selectObj.localPosition = demo.localPosition;
			selectObj.gameObject.SetActive(true);
		}

		private void SetState(State val)
		{
			state = val;
			if (state == State.Running)
			{
				ChangeType(Type.Main);
			}
			else
			{
				ChangeType(Type.None);
			}
		}

		private void ResetSelectObject()
		{
			if (selectObj != null)
			{
				Object.Destroy(selectObj.gameObject);
				selectObj = null;
			}
			selectScript = null;
		}
	}
}
